package android;


import hibernate.Parkstab;
import hibernate.ParkstabDAO;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;



import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Servlet implementation class SearchParkingServlet
 */

public class SearchParkByName extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchParkByName() {
		super();
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	


		request.setCharacterEncoding("utf-8");

		System.out.println("get connection in search by name");
		JSONObject jsonObj=null;
		String receiveStr=null;
		try {
			jsonObj=new JSONObject(request.getParameter("jsonString"));
			receiveStr=jsonObj.getString("name");
			
		} catch (JSONException e1) {
			
			System.out.println("SearchParkByNameServlet: json parse faile");
			e1.printStackTrace();
		}	

		System.out.println(String.format("search by name\n", receiveStr));


		

		List<Parkstab> parkstab=null;
		ParkstabDAO parkstabDAO=new ParkstabDAO();
		parkstabDAO.getSession().beginTransaction();
		try
		{                 
			String hql="from Parkstab as d "
					+ "where d.name like '%"+receiveStr+"%' ";
			Query result=parkstabDAO.getSession().createQuery(hql);
			//result.setString("searchname", receiveStr);
			parkstab=result.list();
		}
		catch(HibernateException e)
		{
			System.out.println("hibernate error in SearchParkingServlet");
			e.printStackTrace();
		}
		finally
		{
			parkstabDAO.getSession().getTransaction().commit();
			parkstabDAO.getSession().close();

		}


		JSONObject resultJson=new JSONObject();
		try {
			resultJson.put("total",new Integer(parkstab.size()).toString());
			JSONArray jsonArray = new JSONArray();
			for (Parkstab p : parkstab) {
				JSONObject temp = new JSONObject();
				temp.put("uuid", p.getUuid());
				temp.put("name", p.getName());
				temp.put("totalSpace", p.getTotalSpace().toString());
				temp.put("emptySpace", p.getEmptySpace().toString());
				temp.put("description", p.getDescription());
				temp.put("address", p.getDescription());
				temp.put("longitude", p.getLongitude());
				temp.put("latitude", p.getLatitude());
				temp.put("price", p.getPrice());
				temp.put("telNumber", p.getTelNumber());
				temp.put("maxOrderTime", p.getMaxOrderTime());
				jsonArray.put(temp);
			}
			resultJson.put("parkData", jsonArray);
		} catch (JSONException e) {
			
			e.printStackTrace();
		}

	

		OutputStream out=response.getOutputStream();
		out.write(resultJson.toString().getBytes("utf-8"));
		out.flush();
		out.close();
		System.out.println(resultJson.toString()+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
	}

}
